package com.azkj.album.view.fragment;

import android.content.res.ColorStateList;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager2.widget.ViewPager2;

import com.azkj.album.R;
import com.azkj.album.adapter.TabAdapter;
import com.azkj.album.dto.GalleryBean;
import com.azkj.album.view.base.BaseFragment;
import com.azkj.album.view.fragment.gallery.GalleryChildFragmentSelect;
import com.bytedance.sdk.open.aweme.utils.LogUtils;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class GalleryFragmentSelect extends BaseFragment implements GalleryChildFragmentSelect.OnGallerySelListener {

    @BindView(R.id.tab_layout)
    TabLayout tab_layout;
    @BindView(R.id.viewPager)
    ViewPager2 viewPager;

    private static final String[] TABS = {"系统贴图", "我的贴图", "我的收藏"};
    private int activeColor = 0;
    private int normalColor = 0;

    private float activeSize = 16f;
    private float normalSize = 14f;
    private TabAdapter adapter;

    private TabLayoutMediator mediator;

    private static GalleryChildFragmentSelect.OnGallerySelListener mListener;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_gallery_select;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    private void initData() {

        activeColor = getActivity().getResources().getColor(R.color.color_blue);
        normalColor = getActivity().getResources().getColor(R.color.color_19);
        activeSize = getActivity().getResources().getDimension(R.dimen.sp_15);
        normalSize = getActivity().getResources().getDimension(R.dimen.sp_15);

        List<BaseFragment> fragmentList = new ArrayList<>();
        fragmentList.add(GalleryChildFragmentSelect.newInstance(GalleryChildFragmentSelect.GALLERY_SYS, this));
        fragmentList.add(GalleryChildFragmentSelect.newInstance(GalleryChildFragmentSelect.GALLERY_MINE, this));
        fragmentList.add(GalleryChildFragmentSelect.newInstance(GalleryChildFragmentSelect.GALLERY_COLLECT, this));

        adapter = new TabAdapter(getActivity(), fragmentList);
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT);
        viewPager.setUserInputEnabled(false);

        mediator = new TabLayoutMediator(tab_layout, viewPager, (tab, position) -> {
            TextView tabView = new TextView(getActivity());
            int[][] states = new int[2][];
            states[0] = new int[]{android.R.attr.state_selected};
            states[1] = new int[]{};

            int[] colors = new int[]{activeColor, normalColor};
            ColorStateList colorStateList = new ColorStateList(states, colors);
            tabView.setText(TABS[position]);
            tabView.setTextSize(TypedValue.COMPLEX_UNIT_PX, normalSize);
            tabView.setTextColor(colorStateList);
            tabView.setGravity(Gravity.CENTER);
            tab.setCustomView(tabView);
        });

        mediator.attach();
        viewPager.registerOnPageChangeCallback(changeCallback);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mediator.detach();
        viewPager.unregisterOnPageChangeCallback(changeCallback);
    }

    private final ViewPager2.OnPageChangeCallback changeCallback = new ViewPager2.OnPageChangeCallback() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            int tabCount = tab_layout.getTabCount();
            mListener.onShow(position);
            for (int i = 0; i < tabCount; i++) {
                TabLayout.Tab tab = tab_layout.getTabAt(i);
                TextView tabView = (TextView) tab.getCustomView();
                tabView.setTextSize(TypedValue.COMPLEX_UNIT_PX, tab.getPosition() == position ? activeSize : normalSize);
            }
        }
    };

    public void setListener(GalleryChildFragmentSelect.OnGallerySelListener listener) {
        this.mListener = listener;
    }

    @Override
    public void onSel(GalleryBean bean, int position, boolean isShow) {
        LogUtils.d("GalleryFragmentSelect", "bean:" + bean);
        if (null != mListener) {
            mListener.onSel(bean, tab_layout.getSelectedTabPosition(), isShow);
        }
    }

    @Override
    public void onShow(int position) {

    }
}
